<?php
define('PAGE_FIRST','&laquo;&laquo;');
define('PAGE_LAST','&raquo;&raquo;');
define('PAGE_PRE','&laquo;');
define('PAGE_NEXT','&raquo;');

class Pager{

	public $rowCount = 0;
	public $pageNo = 1;
	public $pageSize = 20;
	public $pageCount = 0;

	private $valueArray = array();
	private $pageString = 'pagenum';
	private $pageLimit = 10;
	
	public function __construct( $options=array() )
	{

		$this->valueArray = isset($options['valueArray']) ? $options['valueArray'] : $_GET;
		$this->pageLimit = isset($options['pageLimit']) ? intval($options['pageLimit']) : 10;
		$this->pageString = isset($options['pageString']) ? $options['pageString'] : 'pagenum';
		$this->pageSize = isset($options['pageSize']) ? intval($options['pageSize']) : 20;
		$this->rowCount = isset($options['rowCount']) ? intval($options['rowCount']) : 0;

		$this->pageCount = ceil($this->rowCount/$this->pageSize);
		$this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;
		
		$this->pageNo = isset($options['pageNo'])
			?  intval($options['pageNo'])
			: intval(@$_GET[$this->pageString]);
		$this->pageNo = $this->pageNo<=0 ? 1 : $this->pageNo;
		$this->pageNo =	$this->pageNo > $this->pageCount ? $this->pageCount : $this->pageNo;
	}

	private function genURL( $url="/index.php", $param, $value ){
		if( false === strpos($url,'?')) return $url.'?'.urlEncode($param).'='.urlEncode($value);
		else return $url.'&'.urlEncode($param).'='.urlEncode($value);
	}

	public function paginate($url=null){
		$from = $this->pageSize*($this->pageNo-1)+1;
		$from = ($from>$this->rowCount) ? $this->rowCount : $from;
		$to = $this->pageNo * $this->pageSize;
		$to = ($to>$this->rowCount) ? $this->rowCount : $to;
		$size = $this->pageSize;
		$no = $this->pageNo;
		$max = $this->pageCount;
		$total = $this->rowCount;
		$html = $this->genHTML($url);

		return array(
			'from' => $from,
			'to' => $to,
			'size' => $size,
			'no' => $no,
			'max' => $max,
			'total' => $total,
			'html' => $html,
		);
	}

	public function genHTML( $url="/index.php" ){

		if ( null==$url ) $url = $_SERVER['SCRIPT_NAME'];

		foreach($this->valueArray as $key=>$value) {
			if ( $key != $this->pageString ) {
				$url = $this->genURL($url,$key,$value);
			}
		}

		$html = "<div id='pageLink'><span id='pageNavSpace'></span>";

		//show prev page
		$nowInPageLimit = ceil($this->pageNo/$this->pageLimit);
		$nowInPageLimit = $nowInPageLimit < 1 ? 1 : $nowInPageLimit;
		$countInPageLimit = ceil($this->pageCount/$this->pageLimit);

		if( $this->pageNo >  2){
			$html .= "<span id='pageNavSpace'></span><a href=\"".$this->genURL($url,$this->pageString,1)."\">".PAGE_FIRST."</a>";
		}
		if ( $this->pageNo > 1) {
			$pre = $this->pageNo-1;
			$html .= "<span id='pageNavSpace'></span><a href=\"".$this->genURL($url,$this->pageString,$pre)."\">".PAGE_PRE."</a>&nbsp;";
		}
		
		$from = ($nowInPageLimit-1)*$this->pageLimit+1; $from=($from<0)?1:$from;
		$end = $from+$this->pageLimit+1;	

		for($i=$from; $i<$end && $i<=$this->pageCount;$i++){
			if($i==$this->pageNo)
				$html .= "<span id='pageNavSpace'></span><span id='pageLinkOn'>$i</span>";
			else
				$html .= "<span id='pageNavSpace'></span><a href=\"".$this->genURL($url,$this->pageString,$i)."\">$i</a>";
		}
		
		//show next;		
		if($nowInPageLimit < $countInPageLimit){
			$next = ($this->pageNo+1) > $this->pageCount ?  $this->pageCount : ($this->pageNo+1);

			$html .= "<span id='pageNavSpace'></span><a href=\"".$this->genURL($url,$this->pageString,$next)."\">".PAGE_NEXT."</a>";
			$html .= "<span id='pageNavSpace'></span><a href=\"".$this->genURL($url,$this->pageString,$this->pageCount)."\">".PAGE_LAST."</a>";
		}
		$html .="</div>";
		
		return $html;
	}
}
?>
